﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Threading;
using System.Xml.Linq;
using EzDesk.Core.Data;
using EzDesk.Core.ServiceImpl.Services.FeedTransformer;
using EzDesk.Utility.Extensions;
using EzDesk.Utility.EzML;
using EzDesk.Core.Model;
using EzDesk.Utility.Json;
using EzDesk.Core.Interface;
using EzDesk.Utility.Log;
using System.Linq;
using EzDesk.Utility.Extensions;

namespace EzDesk.Core.ServiceImpl.Services
{
    public partial class CoreClientService : ICoreClientService
    {
        #region [ Feed Methods ]

        // 这里的规则是，获取所有的notification，并且获取最多30条的update
        public FeedCollection GetFeeds(Guid userId, DateTime? from, DateTime? to, bool includeNotification = true)
        {
            // LogEx.TRACE("CoreClientService", "InnerGetFeeds 开始");
            var allFeeds = _coreService.GetFeeds(userId, from, to, 30, includeNotification, null, null, null);
            // LogEx.TRACE("CoreClientService", "InnerGetFeeds 结束");
            return generateFeedCollection(allFeeds, userId);
        }

        public void UpdateFeedStatus(List<FeedStatus> feedStatus, string memberString)
        {
            _coreService.UpdateFeedStatus(feedStatus, memberString);
        }

        #endregion

        #region [ private methods ]

        private static FeedCollection transformFeeds(FeedCollection feedCollection, string targetType = "desktop-html")
        {
            var feedTransform = createTransform(targetType);
            feedTransform.Transform(feedCollection, feedCollection.Parameters.ToArray());
            return feedCollection;
        }

        private static FeedTransformBase createTransform(string targetType)
        {
            FeedTransformBase feedTransform;
            switch (targetType)
            {
                case "xaml":
                    feedTransform = new XamlFeedTransform();
                    break;
                case "html":
                    feedTransform = new HtmlFeedTransform();
                    break;
                case "desktop-html":
                    feedTransform = new DesktopHtmlFeedTransform();
                    break;
                default:
                    feedTransform = new HtmlFeedTransform();
                    break;
            }
            return feedTransform;
        }

        private static string getFeedTypeStr(Feed feed)
        {
            return FeedTypeDictionary.IntTypeToString(feed.FeedType);
        }

        private void getFeedExtraFeedData(Feed feed, Guid userId, FeedTemplate feedTemplate,
            out string feedTypeStr,
            out string feedTitle,
            out string feedBody)
        {
            //var personalFeedData = personalFeedsData[feed.Id];
            if (feedTemplate == null)
            {
                LogEx.TRACE("CoreClientService_Feed", "FeedTemplate找不到： " + feed.TemplateId);
            }
            feedTypeStr = getFeedTypeStr(feed);
            feedTitle = feedTemplate.Title;
            feedBody = feedTemplate.Body;
        }

        private FeedCollection generateFeedCollection(IEnumerable<Feed> feeds, Guid userId)
        {
            //LogEx.TRACE("CoreClientService", "generateFeedCollection 开始");
            var allFeedTemplates = _coreService.GetFeedTemplate();
            //LogEx.TRACE("CoreClientService", "拿FeedTemplates 结束");
            var feedCollection = new FeedCollection("http://" + GetClientBaseUrl());
            foreach (var feed in feeds)
            {
                string feedTypeStr;
                string feedTitle;
                string feedBody;
                FeedTemplate feedTemplate = allFeedTemplates.FirstOrDefault(p => p.Id == feed.TemplateId);
                getFeedExtraFeedData(feed, userId, feedTemplate,
                                     out feedTypeStr,
                                     out feedTitle,
                                     out feedBody
                    );
                feedCollection.Add(
                    new FeedClientModel()
                        {
                            FeedId = feed.Id,
                            CreateOn = feed.CreateOn.ToTicks(),
                            UpdateOn = feed.UpdateOn.ToTicks(),
                            LastChangeOn = feed.LastChangeOn.ToTicks(),
                            Type = feedTypeStr,
                            IsRead = feed.IsRead.GetValueOrDefault(false),
                            IsMark = feed.IsMark.GetValueOrDefault(false),
                            IsAvailable = feed.IsAvailable.GetValueOrDefault(false),
                            IsDisplayInMainPanel = feed.IsDisplayInMainPanel,
                            IconUrl = getIconUrl(feedTemplate.TemplateIconName),

                            FeedData = feed.FeedData,
                            PersonalData = feed.PersonalData,

                            OriginalData = new FeedDataModel
                                               {
                                                   Title = feedTemplate.Title,
                                                   Body = feedTemplate.Body,
                                               }
                        });
            }
            //LogEx.TRACE("CoreClientService", "XSLT转换 开始");
            transformFeeds(feedCollection);
            //LogEx.TRACE("CoreClientService", "XSLT转换 结束");
            //LogEx.TRACE("CoreClientService", "generateFeedCollection 结束");
            return feedCollection;
        }
        
        #endregion
    }
}
